package com.ruoyi.business.service.impl.quickrepay;

import com.ruoyi.bizsys.domain.LoanIou;
import com.ruoyi.bizsys.domain.ReduceConfig;
import com.ruoyi.bizsys.mapper.CustReduceDataMapper;
import com.ruoyi.business.Vo.CustReduceRes;
import com.ruoyi.business.service.CustReduceProess;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.BigInteger;

/**
 * @author wqg
 * @description 计算减免金额
 * 减免最小金额<本金*比例>原服务费用
 * @date 2022/4/7
 */
@Slf4j
@Order(4)
@Component
public class ReduceAmtCalcuImpl implements CustReduceProess {
    @Autowired
    CustReduceDataMapper custReduceDataMapper;
    @Override
    public CustReduceRes handle(ReduceConfig reduceConfig, LoanIou loanIou, CustReduceRes custReduceRes) {
        // 剩余本金
        BigDecimal subAmt =  loanIou.getLoanPrincipal().subtract(loanIou.getRepaidPrincipal());
        // 减免金额 应还本金*比例
        BigDecimal reduceAmt = subAmt.multiply(reduceConfig.getRate()).setScale(6, BigDecimal.ROUND_DOWN);
        BigDecimal numBit = reduceAmt.divideAndRemainder(new BigDecimal(10))[1].setScale(6, BigDecimal.ROUND_DOWN);
        reduceAmt =reduceAmt.subtract(numBit).setScale(6, BigDecimal.ROUND_DOWN);
           //等于0，则b1=b2；
          //等于 1 ，则b1>b2 ;
          //等于-1，则b1<b2。
        // 减免金额>配置的减免最小金额
        custReduceRes.setIsHave(true);
        custReduceRes = getCustReduceRes(reduceConfig, custReduceRes, reduceAmt);
        if(!custReduceRes.getIsHave()){
            return custReduceRes;
        }
        custReduceRes = getCustReduceRes(loanIou, custReduceRes, reduceAmt);
        if(!custReduceRes.getIsHave()){
            return custReduceRes;
        }
        return custReduceRes;
    }

    /**
     * 最大最小配置金额判断
     * @param reduceConfig
     * @param custReduceRes
     * @param reduceAmt
     *            //等于0，则b1=b2；
     *           //等于 1 ，则b1>b2 ;
     *           //等于-1，则b1<b2。
     * @return
     */
    private CustReduceRes getCustReduceRes(ReduceConfig reduceConfig, CustReduceRes custReduceRes, BigDecimal reduceAmt) {
        if(reduceAmt.compareTo(reduceConfig.getReduceAmtMin())==-1){
            log.info("CustReduceProess:减免费用小于配置的最小金额，不记录");
            custReduceRes.setIsHave(false);
            return custReduceRes;
        }
        if(reduceAmt.compareTo(reduceConfig.getReduceAmtMax())==1){
            log.info("CustReduceProess:减免费用大于配置的最大金额");
            custReduceRes.setIsHave(false);
            return custReduceRes;
        }
        return custReduceRes;
    }

    private CustReduceRes getCustReduceRes(LoanIou loanIou, CustReduceRes custReduceRes, BigDecimal reduceAmt) {
        // 减免金额 和服务费对比
        if(reduceAmt.compareTo(loanIou.getServiceFee())==-1){
            log.info("CustReduceProess:减免费用小于服务费");
            custReduceRes.setReduceAmont(reduceAmt);
            return custReduceRes;
        }
        // 减免金额 和服务费对比
        if(reduceAmt.compareTo(loanIou.getServiceFee())>=0){
            log.info("CustReduceProess:减免费用大于服务费");
            custReduceRes.setReduceAmont(loanIou.getServiceFee());
            return custReduceRes;
        }
        return custReduceRes;
    }
}
